package com.jackstraw.jack_base.rules.service.impl;

import com.jackstraw.jack_base.datasource.common.aspect.annotation.DataSourceNameCut;
import com.jackstraw.jack_base.datasource.common.entity.enums.DataSourceTypeEnum;
import com.jackstraw.jack_base.datasource.mybatis.base.service.impl.MyBatisServiceImpl;
import com.jackstraw.jack_base.rules.dao.database.ImportPackageDao;
import com.jackstraw.jack_base.rules.entity.bo.ImportPackageBo;
import com.jackstraw.jack_base.rules.entity.bo.RelationRipBo;
import com.jackstraw.jack_base.rules.entity.mapstruct.ImportPackageMapstruct;
import com.jackstraw.jack_base.rules.entity.po.ImportPackageKeyPo;
import com.jackstraw.jack_base.rules.entity.po.ImportPackagePo;
import com.jackstraw.jack_base.rules.entity.po.ImportPackagePoExample;
import com.jackstraw.jack_base.rules.service.ImportPackageService;
import com.jackstraw.jack_base.rules.service.RelationRipService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 导包表  service实现类
 *
 * @author zhaozhao 945514650@qq.com
 * @since version:1.0 generatorDate:2022-12-04 14:28:28
 */
@Slf4j
@DataSourceNameCut(dataSourceName = "dataBaseName1", dataSourceTypeEnum = DataSourceTypeEnum.database)
public class ImportPackageServiceImpl
        extends MyBatisServiceImpl<ImportPackageDao, ImportPackagePo, ImportPackageBo, ImportPackageKeyPo<ImportPackagePo>, ImportPackagePoExample>
        implements ImportPackageService {

    @Autowired
    private ImportPackageMapstruct importPackageMapstruct;

    @Autowired
    private RelationRipService relationRipService;

    @Override
    public List<ImportPackageBo> queryImportSByRuleId(Long ruleId) {
        List<ImportPackagePo> importPackagePos = this.getDao().selectByRuleId(ruleId);
        return importPackageMapstruct.boToS(importPackagePos);
    }

    @Override
    public Map<Long, List<ImportPackageBo>> queryImportSByRuleGroupId(Long ruleGroupId) {
        //查询ruleGroupId相关rule和importpackage的关系
        List<RelationRipBo> relationRipBos = relationRipService.queryAllByRuleGroupId(ruleGroupId);
        List<ImportPackagePo> importPackagePos = this.getDao().selectByRuleGroupId(ruleGroupId);
        List<ImportPackageBo> importPackageBos = importPackageMapstruct.boToS(importPackagePos);
        Map<Long, ImportPackageBo> importPackageBoMapByPackageId
                = importPackageBos.stream().collect(Collectors.toMap(ImportPackageBo::getPackageId, Function.identity()));
        Map<Long, List<ImportPackageBo>> importPackageBosMapByRuleId = relationRipBos.stream()
                .collect(Collectors.groupingBy(RelationRipBo::getRuleId,
                        Collectors.mapping(relationRipBo
                                -> importPackageBoMapByPackageId.get(relationRipBo.getPackageId()), Collectors.toList())));
        return importPackageBosMapByRuleId;
    }
}
